﻿#pragma once
#include <string>
#include <vector>
#include <unordered_map>
#include <mutex>
#include <shared_mutex>
#include <memory>
#include <map>

/**
 * @class StringManager
 * @brief 字符串规则管理器，提供精确匹配和模糊匹配功能
 *
 * 支持两种字符串类型：UTF-8编码字符串和宽字符字符串
 * 提供两种匹配规则：默认规则和追加规则
 * 使用AC自动机实现高效的模糊匹配
 */
class StringManager {
public:
    // 禁用所有构造和赋值操作
    StringManager() = delete;
    StringManager(const StringManager&) = delete;
    StringManager& operator=(const StringManager&) = delete;

    // 初始化管理器
    static void Initialize();

    // 清理资源
    static void Cleanup();      

    // 规则添加
    static void AddDefaultRule(const wchar_t* orig, const wchar_t* repl);
    static void AddAppendRule(const wchar_t* orig, const wchar_t* repl);
    static void AddDefaultRule(const char* orig, const char* repl);
    static void AddAppendRule(const char* orig, const char* repl);
    static void AddFuzzyDefaultRule(const wchar_t* orig, const wchar_t* repl, int priority);
    static void AddFuzzyAppendRule(const wchar_t* orig, const wchar_t* repl, int priority);
    static void AddFuzzyDefaultRule(const char* orig, const char* repl, int priority);
    static void AddFuzzyAppendRule(const char* orig, const char* repl, int priority);

    // 精确查找
    static const char* FindDefault(const char* orig);
    static const char* FindAppend(const char* orig);
    static std::string FindDefault(const std::string& orig);
    static std::string FindAppend(const std::string& orig);

    static const wchar_t* FindDefault(const wchar_t* orig);
    static const wchar_t* FindAppend(const wchar_t* orig);
    static std::wstring FindDefault(const std::wstring& orig);
    static std::wstring FindAppend(const std::wstring& orig);

    // 模糊查找
    static std::string FuzzyMatchDefault(const char* input);
    static std::string FuzzyMatchAppend(const char* input);
    static std::string FuzzyMatchDefault(const std::string& input);
    static std::string FuzzyMatchAppend(const std::string& input);

    static std::wstring FuzzyMatchDefault(const wchar_t* input);
    static std::wstring FuzzyMatchAppend(const wchar_t* input);
    static std::wstring FuzzyMatchDefault(const std::wstring& input);
    static std::wstring FuzzyMatchAppend(const std::wstring& input);


private:
    // 模糊匹配字典结构体（UTF-8编码）
    struct FuzzyUtf8Entry {
        std::string orig;      // 原始字符串
        std::string repl;      // 替换字符串
        bool is_append;        // 是否为追加规则
        int priority;          // 规则优先级
    };

    // 模糊匹配字典结构体（宽字符编码）
    struct FuzzyWideEntry {
        std::wstring orig;     // 原始宽字符串
        std::wstring repl;     // 替换宽字符串
        bool is_append;        // 是否为追加规则
        int priority;          // 规则优先级
    };

    // AC自动机节点
    struct ACNodeUtf8;
    struct ACNodeWide;

    // AC自动机缓存
    struct ACAutoCache {
        std::mutex mutex;
        std::shared_ptr<ACNodeUtf8> utf8_root;
        std::shared_ptr<ACNodeWide> wide_root;
        size_t utf8_dict_hash = 0;
        size_t wide_dict_hash = 0;
    };

    // 精确匹配规则
    static std::unordered_map<std::string, std::string> default_utf8_rules_;
    static std::unordered_map<std::string, std::string> append_utf8_rules_;
    static std::unordered_map<std::wstring, std::wstring> default_wide_rules_;
    static std::unordered_map<std::wstring, std::wstring> append_wide_rules_;

    // 模糊匹配规则
    static std::vector<FuzzyUtf8Entry> fuzzy_utf8_default_dict_;
    static std::vector<FuzzyUtf8Entry> fuzzy_utf8_append_dict_;
    static std::vector<FuzzyWideEntry> fuzzy_wide_default_dict_;
    static std::vector<FuzzyWideEntry> fuzzy_wide_append_dict_;

    // AC自动机缓存
    static ACAutoCache fuzzy_ac_auto_cache_default;
    static ACAutoCache fuzzy_ac_auto_cache_append;

    // 锁
    static std::shared_mutex rules_mutex_; // 规则互斥锁
    static std::shared_mutex fuzzy_mutex_; // 模糊字典互斥锁


    // 辅助函数
    static size_t ComputeFuzzyUtf8DictHash(const std::vector<FuzzyUtf8Entry>& dict);
    static size_t ComputeFuzzyWideDictHash(const std::vector<FuzzyWideEntry>& dict);
    static void SortFuzzyDicts();
    static void ClearFuzzyAutomatonCache();

    // AC自动机构建函数
    static std::shared_ptr<ACNodeUtf8> BuildACAutomatonUtf8(const std::vector<FuzzyUtf8Entry>& dict);
    static std::shared_ptr<ACNodeWide> BuildACAutomatonWide(const std::vector<FuzzyWideEntry>& dict);

    // AC自动机匹配函数
    static std::vector<std::pair<size_t, size_t>> MatchUsingACAutomaton(
        const std::shared_ptr<ACNodeUtf8>& root,
        const std::string& input);

    static std::vector<std::pair<size_t, size_t>> MatchUsingACAutomaton(
        const std::shared_ptr<ACNodeWide>& root,
        const std::wstring& input);

    // 替换函数
    static std::string PerformUtf8SubstringReplacement(
        const std::string& input,
        const std::vector<FuzzyUtf8Entry>& dict);

    static std::wstring PerformWideSubstringReplacement(
        const std::wstring& input,
        const std::vector<FuzzyWideEntry>& dict);
};
